package com.syh.app.basic.views.dialog_list;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.syh.app.basic.R;
import com.syh.app.basic.utils.AppUtil;
import com.syh.app.basic.views.TitleView;

import java.util.List;

/**
 * 创建者：SYH
 * 时间：2020/4/27 9:12
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class ListDialog<T extends DialogListAdp.ShowText> extends Dialog {

    private TitleView titleView;
    private RecyclerView rvData;

    private DialogListAdp mDialogListAdp;
    private Context mContext;
    private WindowManager.LayoutParams mLayoutParams;
    private OnItemSelectListener mOnItemSelectListener;

    public ListDialog(Context context,OnItemSelectListener onItemSelectListener) {
        super(context, R.style.FullDialog);
        this.mOnItemSelectListener = onItemSelectListener;
        initContentView(context);
        init(context);
    }

    public ListDialog(Context context, int themeResId) {
        super(context, themeResId);
        initContentView(context);
        init(context);
    }

    public ListDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        initContentView(context);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_list, null);
        titleView = view.findViewById(R.id.titleView);
        rvData = view.findViewById(R.id.rvData);
        this.setContentView(view);

        mDialogListAdp = new DialogListAdp(mContext, new DialogListAdp.OnItemClickListener() {
            @Override
            public void onSelect(DialogListAdp.ShowText showText) {
                if (mOnItemSelectListener != null) {
                    mOnItemSelectListener.onSelect(showText);
                    ListDialog.this.cancel();
                }
            }
        });
        rvData.setLayoutManager(new LinearLayoutManager(mContext,RecyclerView.VERTICAL,false));
        rvData.setAdapter(mDialogListAdp);

        titleView.setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ListDialog.this.cancel();
            }
        });
    }

    public void build(String title, List<T> data) {
        titleView.setTitleText(title);
        mDialogListAdp.setNewData(data);
    }

    public interface OnItemSelectListener<T extends DialogListAdp.ShowText> {
        void onSelect(DialogListAdp.ShowText item);
    }

    @Override
    public void show() {
        super.show();
    }
}
